'''
    作业：自动取款机的存取款模拟
    要求：有登陆和退出、查询余额、取钱，存钱等操作
    1、银行卡使用.txt文件代替，对比.txt文件模拟插卡
    2、登入
    3、密码管理
    4、查询余额
    5、取钱
    6、存钱
    7、登出
    8、界面和交互
'''
# -添加文件夹、txt文件(模拟银行卡功能)，加载os模块
import os
os.mkdir('./account')
# -添加模拟银行卡（.txt文件代替）
os.mkdir('./bankcards')
bankcard = open('./bankcards/card1.txt','w')
bankcard.write('nameldkpw123456money0000000120000')
bankcard = open('./bankcards/card2.txt','w')
bankcard.write('nameldjpw123123money0000000120000')
bankcard = open('./bankcards/card3.txt','w')
bankcard.write('namelkypw112233money0000000000000')
bankcard.close()
# -添加模拟一个插卡口（putin文件夹代替）
os.mkdir('./putin')

# -模拟银行卡进入自助机
def copy_file(file1, file2):
    f1 = open(file1, 'rb')
    f2 = open(file2, 'wb')
    content = f1.readline()
    while len(content) > 0:
        f2.write(content)
        content = f1.readline()
    f1.close()
    f2.close()


def card_in(dir1, dir2):
    dlist = os.listdir(dir1)
    for f in dlist:
        file1 = os.path.join(dir1, f)
        file2 = os.path.join(dir2, f)
        if os.path.isfile(file1):
            copy_file(file1, file2)
            os.remove(file1)


# -模拟密码环节不输入密码直接退卡
def card_out():
    pw_2 = ('')
    if pw_1 == pw_2:
        card_in('./account', './putin')


# -办理完业务退出退卡
def card_r_out():
    card_in('./account', './putin')


# -密码提示
def pw_show():
    pw_list = os.listdir('./account')
    for f in pw_list:
        card_file = os.path.join('./account', f)
        if os.path.isfile(card_file):
            card_read = open(card_file, 'r')
            card_check = card_read.read(26)
            card_check_pw = card_check[9:15]
            print('密码提示：', card_check_pw)


# -密码输入，判断密码对错
def pw_check():
    global pw_1
    pw_1 = (input(':'))
    pw_list = os.listdir('./account')
    for f in pw_list:
        card_file = os.path.join('./account', f)
        if os.path.isfile(card_file):
            card_read = open(card_file, 'r')
            card_check = card_read.read(26)
            card_check_pw = card_check[9:15]
            card_read.close()
            if card_check_pw == pw_1:
                account_show()
            elif pw_1 == '':
                card_r_out()
            elif card_check_pw != pw_1:
                print('密码输入错误，银行卡收回，请与银行联系')


# -模拟账户界
def name_show():
    name_list = os.listdir('./account')
    for f in name_list:
        card_file = os.path.join('./account', f)
        if os.path.isfile(card_file):
            card_read = open(card_file, 'r')
            card_check = card_read.read(33)
            card_check_pw = card_check[4:7]
            card_read.close()
            print(card_check_pw,end='')


def account_show():
    while True:
        print('=' * 12,end='')
        name_show()
        print(',欢迎您使用银行自助机', '=' * 12)
        print('请选择要办理的业务：')
        print('{:8} {:16} {:10}'.format(' ', '1.查看账户余额', '2.存款'))
        print('{:8} {:16} {:10}'.format(' ', '3.修改账户密码', '4.取款'))
        print(' ' * 20, '5.退出')
        print('=' * 49)
        key = int(input('请输入对应的选择：'))
        if key == 1:
            account_money_show()
        elif key == 3:
            account_pw_check()
        elif key == 2:
            card_money_in()
        elif key == 4:
            card_money_out()
        elif key == 5:
            input('按回车退出并取回您的银行卡')
            card_r_out()
            break
        else:
            print('请输入正确的选项')
            pass


# -查询余额、存取钱、修改密码、退出
def account_money_show():
    pw_list = os.listdir('./account')
    for f in pw_list:
        card_file = os.path.join('./account', f)
        if os.path.isfile(card_file):
            card_read = open(card_file, 'r')
            card_check = card_read.read(33)
            card_check_pw = card_check[20:33]
            card_read.close()
            print('您的余额为：', float(card_check_pw))


def account_pw_check():
    while True:
        pw_1 = (input('请输入旧密码:'))
        pw_list = os.listdir('./account')
        for f in pw_list:
            card_file = os.path.join('./account', f)
            if os.path.isfile(card_file):
                card_read = open(card_file, 'r')
                card_check = card_read.read(26)
                card_check_pw = card_check[9:15]
                card_read.close()
                if card_check_pw != pw_1:
                    account_pw_check('./account')
                if card_check_pw == pw_1:
                    pw_2 = input('请输入六位数新密码：')
                    pw_3 = input('请再次确认六位数新密码：')
                    if len(pw_3) > 6 or len(pw_3) < 6:
                        print('密码为6位数数字')
                        account_pw_check('./account')
                    elif pw_2 != pw_3:
                        print('前后密码输入不一致')
                        account_pw_check('./account')
                    elif pw_2 == pw_3:
                        pw_list = os.listdir('./account')
                        for f in pw_list:
                            card_file = os.path.join('./account', f)
                            if os.path.isfile(card_file):
                                card_read = open(card_file, 'r+')
                                # -seek移动光标到指定位置
                                card_read.seek(9)
                                card_read.write(pw_3)
                                card_read.close()
                                print('您的密码修改完成')
        break


def card_money_in():
    cmi_1 = int(input('请输入您要存入的金额:'))
    while True:
        if cmi_1 <= 0:
            print('请输入正确的金额')
            card_money_in()
        else:
            cmi_2 = cmi_1
            money_list = os.listdir('./account')
            for f in money_list:
                card_file = os.path.join('./account', f)
                if os.path.isfile(card_file):
                    card_read = open(card_file, 'r+')
                    card_check = card_read.read(33)
                    card_check_pw = card_check[20:33]
                    cmi_3 = int(card_check_pw)
                    card_read.close()
            cmi_4 = cmi_2 + cmi_3
            cmi_5 = str(cmi_4)
            cmi_6 = len(cmi_5)
            pw_list = os.listdir('./account')
            for f in pw_list:
                card_file = os.path.join('./account', f)
                if os.path.isfile(card_file):
                    card_read = open(card_file, 'r+')
                    card_read.seek(33 - cmi_6)
                    card_read.write(cmi_5)
                    card_read.close()
                    print('您的余额为：', float(cmi_4))
            break


def card_money_out():
    money_list = os.listdir('./account')
    for f in money_list:
        card_file = os.path.join('./account', f)
        if os.path.isfile(card_file):
            card_read = open(card_file, 'r+')
            card_check = card_read.read(33)
            card_check_pw = card_check[20:33]
            cmi_3 = card_check_pw
            card_read.close()
    print('您的余额为：', float(cmi_3))
    cmi_1 = int(input('请输入您要取出的金额:'))
    while True:
        if cmi_1 <= 0:
            print('请输入正确的金额')
            card_money_out()
        else:
            cmi_2 = cmi_1
            money_list = os.listdir('./account')
            for f in money_list:
                card_file = os.path.join('./account', f)
                if os.path.isfile(card_file):
                    card_read = open(card_file, 'r+')
                    card_check = card_read.read(33)
                    card_check_pw = card_check[20:33]
                    cmi_3 = int(card_check_pw)
                    card_read.close()
            cmi_4 = cmi_3 - cmi_2
            if cmi_4 >= 0:
                cmi_5 = str(cmi_4)
                cmi_6 = len(cmi_5)
                pw_list = os.listdir('./account')
                for f in pw_list:
                    card_file = os.path.join('./account', f)
                    if os.path.isfile(card_file):
                        card_read = open(card_file, 'r+')
                        card_read.seek(33 - cmi_6)
                        card_read.write(cmi_5)
                        card_read.close()
                        print('您的余额为：', float(cmi_4))
                break
            else:
                print('您的余额不足')
                card_money_out()


while True:
    print('=' * 19, '请插入银行卡', '=' * 19)
    print(' 请手动把bankcard文件夹下.txt文件复制到putin文件夹下')
    print(' ' * 18, '模拟银行卡插入')
    print('=' * 51)
    input('请插入银行卡回车继续...')
    find_card = os.listdir('./putin')
    size = 0
    for f in find_card:
        file1 = os.path.join('./putin', f)
        if os.path.isfile(file1):
            os.path.getsize(file1)
            size = os.path.getsize(file1)
    if size >= 6:
        card_in('./putin', './account')
        print('=' * 13, '请输入密码, 直接按回车退卡', '=' * 12)
        pw_show()
        # card_out()
        pw_check()
    elif size < 6:
        continue






